﻿#include "my_system.h"
#include "time.h"


_system_data system_data;

int system_init(void)
{
	system_data.Language = JianTiZhongWen;


	sprintf(system_data.wifi_name,"Xiaomi_GSM");
	sprintf(system_data.wifi_password,"17625349864");
	sprintf(system_data.productname,PRODUCT_NAME);
	sprintf(system_data.bilibili_id,"NULL");
	sprintf(system_data.IP_ADDR,lvgl_globa_text[42][system_get_Language()]);

	system_set_wifi_kaiguan(true);
	system_set_radio_onoff(1);
	system_set_radio_vol(10);
	system_set_radio_sig(6);
	system_set_radio_frequency(8910);

#if USE_WIN == 1

	system_set_radio_p(0,"88.8");
	system_set_radio_p(1,"89.1");
	system_set_radio_p(2,"95.7");
	system_set_radio_p(3,"98.2");
	system_set_radio_p(4,"102.8");


#endif


    return 0;
}
void system_give_https_request_Semaphore(void)
{
#if USE_WIN == 0
	xSemaphoreGive(system_data.https_request_Semaphore);
#endif
}
void system_set_radio_p(uint8_t index,char *data)
{
	sprintf(system_data.radio_data.radio_p[index],"%s",data);
}
char *system_get_radio_p(uint8_t index)
{
	return system_data.radio_data.radio_p[index];
}


int system_set_Language(int dat)
{
	int Language = system_data.Language;
	system_data.Language = dat;
	system_data.uart_rx_flag=0;

	return Language;
}




int system_get_Language(void)
{
	return system_data.Language;
}
int system_set_backlight(int dat)
{
	system_data.backlight = dat;

#if USE_WIN == 0
	bsp_ledc_set_duty(system_data.backlight);

#endif
	return system_data.backlight;
}

int system_get_backlight(void)
{
	return system_data.backlight;
}



int system_set_wifi_kaiguan(int dat)
{
	int dat1 = system_data.wifi_kaiguan;
	system_data.wifi_kaiguan = dat;
	return dat1;
}

int system_get_wifi_kaiguan(void)
{
	return system_data.wifi_kaiguan;
}

uint32_t system_get_file_size(char * path)
{
	//uint32_t fileSize=0;
	//FILE *pread = fopen(path,"rb");
	//if (pread==NULL)
	//{
	//	system_debug("文件打开失败");
	//	return;
	//}

	//fseek(pread, 0, SEEK_END);
	//fileSize = ftell(pread);
	//fseek(pread,0,SEEK_SET);
	//fclose(pread);
	//system_debug("文件:%s 大小:%d",path,fileSize);
	//return fileSize;
}
void system_set_spk_switch(bsp_spk_switch data)
{
	system_data.spk_switch = data;
#if USE_WIN == 0
	bsp_spk_sw_set(system_data.spk_switch);  //频率设置
#endif

	system_debug("set spk switch:%d",data);
}
void system_audio_select(uint8_t data)
{
	system_data.audio_select = data;
#if USE_WIN == 0
	bsp_audio_select(system_data.spk_switch);  //频率设置
#endif
	if(data == 0)
	{

		system_debug("audio selece radio");
	}else
	if(data == 1)
	{

		system_debug("audio selece i2s");
	}
}


int system_get_radio_vol(void)
{
	return system_data.radio_data.vol;
}

void system_set_radio_vol(int data)
{
	system_data.radio_data.vol = data;
	if(system_data.radio_data.vol == 0)
	{
#if USE_WIN == 0
		rda5807_mute_set(1);
		system_set_spk_switch(SPK_SWITCH_OFF);
#endif
	}else
	{
#if USE_WIN == 0
		system_set_spk_switch(SPK_SWITCH_ON);
		rda5807_mute_set(0);
        rda5807_vol_set(system_data.radio_data.vol);
#endif

	}
	system_debug("set vol:%d",data);
}
void system_set_radio_mute(int data)
{
	system_data.radio_data.mute = data;
	if(system_data.radio_data.mute == 0)
	{
#if USE_WIN == 0
		rda5807_mute_set(0);
#endif

		system_debug("mute close");
	}else
	{
#if USE_WIN == 0
		rda5807_mute_set(1);
#endif
		system_debug("mute open");
	}
}

int system_get_radio_sig(void)
{
	return system_data.radio_data.set_sig;
}

void system_set_radio_sig(int data)
{
	system_data.radio_data.set_sig = data;
#if USE_WIN == 0
		rda5807_seekth_set(system_data.radio_data.set_sig);
#endif


	system_debug("set sig:%d",data);
}
int system_set_radio_next_search(void)
{
	system_debug("next search");
	int temp  = 0;
	system_data.radio_data.serrch_status = false;
#if USE_WIN == 0
		rda5807_seek_direction(0);
		temp = rda5807_seek_channel();
#else
	temp = rand()%2100+8700;
	system_set_radio_frequency(temp);
#endif


	return temp;

}
int system_set_radio_last_search(void)
{
	system_debug("last search");
	int temp  = 0;
	system_data.radio_data.serrch_status = false;
#if USE_WIN == 0
		rda5807_seek_direction(1);
		temp = rda5807_seek_channel();
#else
	temp = rand()%2100+8700;
	system_set_radio_frequency(temp);
#endif
	return temp;
}
void system_set_radio_search_status(uint8_t data)
{

	system_data.radio_data.serrch_status = data;
}
uint8_t system_get_radio_search_status(void)
{

	return system_data.radio_data.serrch_status;
}


int system_get_radio_frequency(void)
{
#if USE_WIN == 0
	system_data.radio_data.frequency = rda5807_freg_get()*10;
#endif

	return system_data.radio_data.frequency;
}

void system_set_radio_frequency(int data)
{
	system_data.radio_data.frequency = data;
#if USE_WIN == 0
	rda5807_freq_set(system_data.radio_data.frequency);  //频率设置
#endif

	system_debug("set frequency:%d",data);
}

void system_set_radio_onoff(int data)
{
	system_data.radio_data.onoff = data;
	if(system_data.radio_data.onoff == 0)
	{
		system_debug("close radio");
#if USE_WIN == 0
	rda5807_enable(0);
#endif


	}else
	if(system_data.radio_data.onoff == 1)
	{
		system_debug("open radio");
#if USE_WIN == 0
	rda5807_enable(1);
#endif

	}
}

int system_get_radio_onoff(void)
{
	return system_data.radio_data.onoff;
}

void system_power_off(void)
{
	system_debug("power off");
#if USE_WIN == 0
	bsp_power_send_off();
#endif

}
uint8_t system_get_power_on_type(void)
{
	system_debug("get power on type");
#if USE_WIN == 0
	return bsp_power_get_on_type();
#endif
	return BSP_POWER_ON_TYPE_USB;
}

void system_set_clock_style(int data)
{
	system_data.clock_data.style = data;
}
int system_get_clock_style(void)
{
	return system_data.clock_data.style;
}
